import Actor from "@/Actor";
import DataBase from "@/DataBase";
import ActorItem from "@/ActorItem";
import ActorAbility from "@/ActorAbility";
import ActorUnit from "@/ActorUnit";

export default class ActorUtil {


    static hasActor(templateId: string, actorTypeId?: string): boolean {
        let actor: Actor = Actor.getByTemplateId(templateId);
        if (actor == null) {
            return false
        }
        if (actorTypeId != null && actorTypeId != actor.getActorTypeId()) {
            return false;
        }
        return true;
    }

    static ifHasActor(templateId: string, callBack: (actor: Actor) => void, actorTypeId?: string): void {
        let actor: Actor = Actor.getByTemplateId(templateId);
        if (actor == null) {
            return
        }
        if (actorTypeId != null && actorTypeId != actor.getActorTypeId()) {
            return;
        }
        callBack(actor);
    }

    static ifHasActorAbility(templateId: string, callBack: (actor: ActorAbility) => void, actorTypeId?: string) {
        let actor: ActorAbility = Actor.getByTemplateId(templateId);
        if (actor == null) {
            return
        }
        if (actorTypeId != null && actorTypeId != actor.getActorTypeId()) {
            return;
        }
        callBack(actor);
    }

    static ifHasActorItem(templateId: string, callBack: (actor: ActorItem) => void, actorTypeId?: string) {
        let actor: ActorItem = Actor.getByTemplateId(templateId);
        if (actor == null) {
            return
        }
        if (actorTypeId != null && actorTypeId != actor.getActorTypeId()) {
            return;
        }
        callBack(actor);
    }

    /**
     * 如果单位是一个 演员单位
     * @param unit
     * @param callBack
     * @param actorTypeId
     */
    static ifHasActorUnit(unit: unit, callBack: (actor: ActorUnit) => void, actorTypeId?: string) {
        let actor: ActorUnit = DataBase.getUnitSolarData(unit)._SL_solarActorUnit;
        if (actor == null) {
            return
        }
        if (actorTypeId != null && actorTypeId != actor.getActorTypeId()) {
            return;
        }
        callBack(actor);
    }

    /**
     * 获取一个单位身上的所有 演员技能
     * @param unit
     */
    static getUnitActorAbilityList(unit: unit): ActorAbility[] {
        let abilityTemplate = DataBase.getUnitSolarData(unit, false)?._SL_abilityTemplate;
        if (abilityTemplate == null) {
            return null;
        }
        let actorList = []
        for (let abilityTemplateKey in abilityTemplate) {
            let actor = Actor.templateIdActorMap[abilityTemplate[abilityTemplateKey]];
            if (actor != null) {
                actorList.push(actor)
            }
        }
        return actorList;
    }

    /**
     * 判断单位是有拥有某个类型的 演员技能
     * @param unit
     * @param actorTypeId
     */
    static isUnitHasActorAbility(unit: unit, actorTypeId: string) {
        let abilityTemplate = DataBase.getUnitSolarData(unit, false)?._SL_abilityTemplate;
        if (abilityTemplate == null) {
            return false;
        }
        for (let abilityTemplateKey in abilityTemplate) {
            let actor = Actor.getByTemplateId(abilityTemplate[abilityTemplateKey]);
            if (actor != null && actor.getActorTypeId() == actorTypeId) {
                return true;
            }
        }
        return false;
    }

    /**
     * 销毁一个单位身上的所有 演员技能
     * @param unit
     */
    static destroyUnitAllActorAbility(unit: unit): void {
        let abilityTemplate = DataBase.getUnitSolarData(unit, false)?._SL_abilityTemplate;
        if (abilityTemplate == null) {
            return null;
        }
        for (let abilityTemplateKey in abilityTemplate) {
            let actor = Actor.getByTemplateId(abilityTemplate[abilityTemplateKey]);
            if (actor != null) {
                actor.destroy();
            }
        }
    }

    /**
     * 获取一个单位身上的所有 演员物品
     * @param unit
     */
    static getUnitActorItemList(unit: unit): ActorItem[] {
        let actorList = []
        for (let i = 0; i < 6; i++) {
            let item = UnitItemInSlot(unit, i);
            if (IsHandle(item)) {
                let actor = Actor.getByTemplateId(id2string(GetItemTypeId(item)));
                if (actor != null) {
                    actorList.push(actor)
                }
            }
        }
        return actorList;
    }

    /**
     * 从基础物品获得 演员物品 (如果该物品不是演员物品则返回null)
     * @param item
     */
    static getActorItemByItem(item: item): ActorItem {
        return Actor.getByTemplateId(id2string(GetItemTypeId(item)));
    }

    /**
     * 判断单位是否持有某个类型的演员物品
     * @param unit
     * @param actorTypeId
     */
    static isUnitHasActorItem(unit: unit, actorTypeId: string) {
        for (let i = 0; i < 6; i++) {
            let item = UnitItemInSlot(unit, i);
            if (IsHandle(item)) {
                let actor = Actor.getByTemplateId(id2string(GetItemTypeId(item)));
                if (actor != null && actor.getActorTypeId() == actorTypeId) {
                    return true;
                }
            }
        }
        return false
    }






}